var play = true;

function timer() {
    refreshTime --;
    if (refreshTime === 0) {
        $("#progressbar").progressbar('option', 'value', 0);
        window.location = window.location;
    } else {
        progress = parseInt(refreshTime  / refreshTimeSetting  * 100);
        $("#progressbar").progressbar('option', 'value', progress);
    }
}

// play/pause the refresh timer
function controls() {
    var src = $("#pause").attr("src");
    if (play) {
        // pause
        play = false;
        clearInterval(interval);
    } else {
        // resume
        play = true;
        interval = setInterval(timer, 1000);
    }
}

function settings() {
    $("#dialog").dialog('open');
    // stop the refresh
    clearInterval(interval);
}

function dialogClose() {
    // set the refresh cookie
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + 30);
    document.cookie = "refresh=" +$("#slider").slider("value") + ";expires=" + exdate.toGMTString();

    // close the dialog
    $("#dialog").dialog("close");

    // resume the refresh
    refreshTime = 1;
    timer();
}

$(document).ready(function() {
    $("#tabs").tabs();
    $("#progressbar").progressbar({value: 100 });
    $("#dialog").dialog({ autoOpen: false, resizable: false, buttons: { "Ok": dialogClose }, modal: true });
    $(function() {
        $("#slider").slider({
            value:refreshTime,
            min: 1,
            max: 60,
            step: 1,
            slide: function(event, ui) {
                $("#amount").val(ui.value);
            }
        });
        $("#amount").val($("#slider").slider("value"));
    });

    interval = setInterval(timer, 1000);
});
